let box = document.querySelector('.box');
let headul = $('.headul li');

// headul划入的效果
headul.hover(function () {
    // $(this).addClass('act').siblings().removerClass('act');
    $(this).addClass('act').siblings().removeClass('act');
}, function () {
    // console.log(1);
});

// 回到顶部
$(window).scroll(function () {
    if (scrollY > 220) {
        $('.top').show();
    } else {
        $('.top').hide();
    }
});
$('.top').click(function () {
    $('html').animate({
        scrollTop: 0
    }, 1000);
});

// 搜索
let searchkbtn = document.querySelector('#searchkbtn');
let searchkey = document.querySelector('#searchkey');
searchkbtn.onclick = function () {
    localStorage.setItem('key',searchkey.value);
    location.href = `../html/list.html`;
}



// banner图
var mySwiper = new Swiper('.swiper-container', {
    direction: 'horizontal', // 垂直切换选项
    loop: true, // 循环模式选项
    speed: 2500,
    autoplay: {
        delay: 2000,
        disableOnInteraction: false,
        waitForTransition: true,
        stopOnLastSlide: false,
    },
    // 如果需要分页器
    pagination: {
        el: '.swiper-pagination',
    },

    // 如果需要前进后退按钮
    navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
    },

    // 如果需要滚动条
    scrollbar: {
        el: '.swiper-scrollbar',
    },
})
// Banner();



// login
let denglu = document.querySelector('#login');
let login = getCookie('login');
login ? denglu.innerHTML = login : '';
denglu.onclick = function () {
    // console.log(location.href);
    location.href = '../html/login.html';
    localStorage.setItem('url',location.href);
    return;
}


// 退出登录
login ? $('.exitdegnlu').show() : $('.exitdegnlu').hide();
$('.exitdegnlu').click(function () {
    console.log(1);
    setCookie('login', '111', '-1');
    location.href = '../html/login.html';
    localStorage.setItem('url',location.href);
    return;
});


// 跳转购物车
$('.car').click(function () {
    if (!login) {
        alert('请先登录，在2s后跳转到登录界面');
        setTimeout(() => {
             location.href = '../html/login.html';
        }, 1000);
        return;
    } else {
        location.href = '../html/mycar.html';
    }

});


// 秒杀
let newdate = new Date();
newdate = newdate.getTime() + 1000 * 60 * 60 * 3;
newdateend = new Date();
newdateend.setTime(newdate);
let timeend = format(newdateend);
$('.date').html(`${timeend.hours}:${timeend.minutes}`);

killtime();

function killtime() {
    let date1 = new Date();
    let date2 = new Date(newdate);
    let time1 = date1.getTime();
    let time2 = date2.getTime();
    let res = fun(time1, time2);
    // console.log(res);
    $('.hour').html(res.hour);
    $('.min').html(res.mins);
    $('.second').html(res.sec);
    if (res.hour == 0 && res.mins == 0 && res.sec == 0) {
        clearInterval(expire);
    }
}
var expire = setInterval(killtime, 1000);


// 秒杀内的滚动图
let scrollimg = document.querySelector('.scrollimg');
async function simg() {
    let res = await pAjax({
        url: '../JSON/data1.json',
    });
    res = JSON.parse(res);
    // console.log(res);
    Runder(res);
}
simg();

function Runder(data) {
    let str = '';
    let str1 = '';
    data.forEach((item, index) => {
        if (index % 2 == 0) {
            str += `<div class="scrloop">
            <img src="${item.img}" alt="">
            <p>${item.nm}</p>

        </div>`;
        } else {
            str += `
        <div class="scrloop">
                    <p>${item.nm}</p>
                    <img src="${item.img}" alt="">
                </div>
        
        `;
        }
    });
    scrollimg.innerHTML = str;
}
let scrollimgleft = 0;
setInterval(function () {
    scrollimgleft += 0.2;
    // console.log(parseInt(Math.random()*255));
    $('.scrollimg').css({
        'transform': `translateX(${-scrollimgleft}px)`
    });
    if (scrollimgleft >= 1550) {
        scrollimgleft = 0;
    }
}, 10);



// list12  
let start = 0,
    end = 12;
let recommendlist = document.querySelector('.recommendlist');
async function list12() {
    // console.log(start, end);
    let res = await pAjax({
        url: '../../6.9list12.php',
        type: 'post',
        data: {
            start: start,
            end: end,
        }
    });
    res = JSON.parse(res);
    console.log(res);
    Runderlist12(res.list);
}
list12();

function Runderlist12(data) {
    let str = '';
    data.forEach(item => {
        str += `
        <a href="../html/detail.html?id=${item.goods_id}">
                <div class="tu">
                    <img src="${item.goods_small_logo}" alt="">
                </div>
                <h4>${item.cat_one_id}</h4>
                <p>${item.goods_name}</p>
            </a>
        
        `;
    });
    recommendlist.innerHTML += str;
}

// 滑动条到底部加载8条数据
window.onscroll = function () {
    // console.log(box.scrollHeight);
    // console.log($(window).scrollTop());
    // console.log($(window).height());
    let screenbottom = $(window).height() + $(window).scrollTop();
    if (box.scrollHeight - screenbottom<=0) {
        // console.log(1);
        start += end;
        end = 8;
        // console.log(start,end);
        list12();
    }
    
}